//
//  PlaceViewController.m
//  HeartTravel
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PlaceViewController.h"
#import "UIImageView+WebCache.h"
#import "MyCell.h"
#import "HTTPTool.h"
#import "ImageViewController.h"
#import "RecDetailViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PlaceViewController ()<MBProgressHUDDelegate>
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, assign)BOOL isLike;
@property(nonatomic, retain)MBProgressHUD *hud;
@property(nonatomic, copy)NSString *upUrl;
@property(nonatomic, copy)NSString *leftUrl;
@property(nonatomic, copy)NSString *rightUrl;

@end

@implementation PlaceViewController

- (void)dealloc{
    [_hud release];
    [_imageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    NSLog(@"%@",self.bId);
    [self createData];
    [self createView];
    [self createHud];
    
}

- (void)createHud{
    self.hud = [[MBProgressHUD alloc] init];
    [self.hud show:YES];
    self.hud.labelText = @"加载中";
    [self.view addSubview:self.hud];
    [self.view bringSubviewToFront:self.hud];
    [self.hud release];
    self.hud.delegate = self;
    
}


//解析数据
- (void)createData{
    
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.bId] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        //        解析imageView的图片
        NSArray *hottest_placesArr = dic[@"hottest_places"];
        NSDictionary *photoDic = hottest_placesArr[0];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoDic[@"photo"]]];
        
        //        解析nameLabel数据
        UILabel *nameLabel = (UILabel *)[self.imageView viewWithTag:1002];
        nameLabel.text = dic[@"name_orig"];
        self.title = dic[@"name_orig"];
        //        解析wentAndLikeLabel数据
        UILabel *wentAndLikeLabel = (UILabel *)[self.imageView viewWithTag:1003];
        wentAndLikeLabel.text = [NSString stringWithFormat:@"%@ 去过 / %@ 喜欢",dic[@"visited_count"],dic[@"wish_to_go_count"]];
        
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    [HTTPTool get:@"http://open.qyer.com/qyer/recommands/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.87990051040604&lon=121.5420294572783&page=1&track_app_channel=App%2520Store&track_app_version=6.4&track_device_info=iPhone%25205s&track_deviceid=93233B2B-D60D-49D5-8024-F18413EAF5FE&track_os=ios%25208.2&v=1" body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSDictionary *dataDic = dic[@"data"];
        NSArray *subjectArr = dataDic[@"subject"];
        self.upUrl = subjectArr[0][@"url"];
        self.leftUrl = subjectArr[1][@"url"];
        self.rightUrl = subjectArr[2][@"url"];
        
        UIView *view = [self.view viewWithTag:1004];
        UIImageView *upView = (UIImageView *)[view viewWithTag:1005];
        UIImageView *leftView = (UIImageView *)[view viewWithTag:1006];
        UIImageView *rightView = (UIImageView *)[view viewWithTag:1007];
        
        [upView sd_setImageWithURL:[NSURL URLWithString:subjectArr[0][@"photo"]]];
        [leftView sd_setImageWithURL:[NSURL URLWithString:subjectArr[1][@"photo"]]];
        [rightView sd_setImageWithURL:[NSURL URLWithString:subjectArr[2][@"photo"]]];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)createView{
    
//    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (HEIGHT - 46 - 49)/ 2)];
    [self.view addSubview:view];
    [view release];
    view.tag = 1001;

//  创建imageView
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, (HEIGHT - 49 - 64) / 2)];
    [view addSubview:self.imageView];
    [self.imageView release];
    
//    创建nameLabel接收旅游地名
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.imageView.frame.size.height - 50, 120, 30)];
    [self.imageView addSubview:nameLabel];
    [nameLabel release];
    nameLabel.font = [UIFont systemFontOfSize:20];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.tag = 1002;
    
//   创建label接收去过人数和喜欢人数
    UILabel *wentAndLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.imageView.frame.size.height - 20, 180, 20)];
    [self.imageView addSubview:wentAndLikeLabel];
    [wentAndLikeLabel release];
    wentAndLikeLabel.font = [UIFont systemFontOfSize:10];
    wentAndLikeLabel.textColor = [UIColor whiteColor];
    wentAndLikeLabel.tag = 1003;
    
//  创建发现下一站
//    标题 下一站
    UIView *nextStationView = [[UIView alloc] initWithFrame:CGRectMake(0, (HEIGHT - 64 - 49) / 2, WIDTH , (HEIGHT - 64 - 49) / 2)];
    [self.view addSubview:nextStationView];
    [nextStationView release];
    nextStationView.tag = 1004;
    
    UILabel *nextStationLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, 20)];
    nextStationLabel.text = @"发现下一站";
    [nextStationView addSubview:nextStationLabel];
    nextStationLabel.font = [UIFont systemFontOfSize:14];
    nextStationLabel.alpha = 0.7;
    [nextStationLabel release];
    
//    三个imageView
//    1
    UIImageView *upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 20, WIDTH - 10, ((HEIGHT - 64 - 49) / 2 - 40) / 2)];
    [nextStationView addSubview:upImageView];
    [upImageView release];
    upImageView.tag = 1005;
    
//    2
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 30 + ((HEIGHT - 64 - 49) / 2 - 40) / 2, (WIDTH - 20) / 2, ((HEIGHT - 64 - 49) / 2 - 40) / 2)];
    [nextStationView addSubview:leftImageView];
    [leftImageView release];
    leftImageView.tag = 1006;
    
//    3
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 + (WIDTH - 20) / 2 + 10, 30 + ((HEIGHT - 64 - 49) / 2 - 40) / 2, (WIDTH - 20) / 2, ((HEIGHT - 64 - 49) / 2 - 40) / 2)];
    [nextStationView addSubview:rightImageView];
    [rightImageView release];
    rightImageView.tag = 1007;
    
//    给三个imageView添加手势
    upImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *threeTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(threeTap1:)];
    [upImageView addGestureRecognizer:threeTap1];
    
    leftImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *threeTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(threeTap2:)];
    [leftImageView addGestureRecognizer:threeTap2];
    
    rightImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *threeTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(threeTap3:)];
    [rightImageView addGestureRecognizer:threeTap3];
    [threeTap1 release];
    [threeTap2 release];
    [threeTap3 release];
    
//   喜欢按钮
    UIBarButtonItem *likeButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xihuan1"] style:UIBarButtonItemStylePlain target:self action:@selector(like:)];
//   分享按钮
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
    NSArray *buttonArr = [NSArray arrayWithObjects:likeButton,shareButton,nil];
    self.navigationItem.rightBarButtonItems = buttonArr;
    [shareButton release];
    [likeButton release];
    self.isLike = NO;
    
    
    //    imageView设置点击方法
    self.imageView.userInteractionEnabled = YES;
//    添加点击方法
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.imageView addGestureRecognizer:tap];

    

    
}

- (void)threeTap1:(UITapGestureRecognizer *)tap{
    RecDetailViewController *recVC = [[RecDetailViewController alloc] init];
    [self.navigationController pushViewController:recVC animated:YES];
    recVC.url = self.upUrl;
    [recVC release];
}
- (void)threeTap2:(UITapGestureRecognizer *)tap{
    RecDetailViewController *recVC = [[RecDetailViewController alloc] init];
    [self.navigationController pushViewController:recVC animated:YES];
    recVC.url = self.leftUrl;
    [recVC release];
}
- (void)threeTap3:(UITapGestureRecognizer *)tap{
    RecDetailViewController *recVC = [[RecDetailViewController alloc] init];
    [self.navigationController pushViewController:recVC animated:YES];
    recVC.url = self.rightUrl;
    [recVC release];
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    ImageViewController *imageView = [[ImageViewController alloc] init];
    [self.navigationController pushViewController:imageView animated:YES];
    imageView.bId = self.bId;
    imageView.type = self.type;
    NSLog(@"%@",self.bId);
    NSLog(@"%@",self.type);
    imageView.titleName = self.title;
//    NSLog(@"%@",self.type);
    [imageView release];
}

//喜欢按钮方法
- (void)like:(UIButton *)button{
    if (self.isLike == NO) {
        self.isLike = YES;
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xihuanjiaohu"]];
    }else if(self.isLike == YES){
      [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xihuan1"]];
        self.isLike = NO;
    }
}
//分享按钮方法
- (void)share:(UIButton *)button{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
